//
//  LFBTabBarViewController.m
//  LoveFreshBee
//  Created by 涂永江 on 2017/7/9.
//  Copyright © 2017年 涂永江. All rights reserved.
//

#import "LFBTabBarViewController.h"
#import "LFBHomeViewController.h"
#import "LFBMineViewController.h"
#import "LFBShopViewController.h"
#import "LFBSupermarketViewController.h"
#import "LFBNavViewController.h"

@interface LFBTabBarViewController ()<UITabBarControllerDelegate>

@property (nonatomic,assign) NSInteger  indexFlag;

@end

@implementation LFBTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.indexFlag = 0;
    
    self.delegate = self;
    // 创建一个可变数组
    NSMutableArray *mutable = [NSMutableArray array];
    
    [mutable addObject:[self addChildControllerWith:@"LFBHomeViewController" title:@"首页" normalImage:@"v2_home"]];
    
    [mutable addObject:[self addChildControllerWith:@"LFBSupermarketViewController" title:@"闪电超市" normalImage:@"v2_order"]];
    
    [mutable addObject:[self addChildControllerWith:@"LFBShopViewController" title:@"购物车" normalImage:@"shopCart"]];
    
     [mutable addObject:[self addChildControllerWith:@"LFBMineViewController" title:@"我的" normalImage:@"v2_my"]];
    
    // 为viewControllers赋值
    self.viewControllers = mutable;
    
}

#pragma mark
#pragma mark - 实例化子控制器
- (LFBNavViewController *)addChildControllerWith:(NSString *)clsName title:(NSString *)title normalImage:(NSString *)normalImage  {
    
    // 字符串转为类
    Class cls = NSClassFromString(clsName);
    
    // 实例化控制器
    UIViewController *controller = [[cls alloc] init];
    
    // 设置控制器
    // title
    controller.title = title;
    
    // tabbar的图片
    controller.tabBarItem.image = [UIImage imageNamed:normalImage];
    
    // 被选中的image
    NSString *imageString = [normalImage stringByAppendingString:@"_r"];
    
    // 实例化图片
    UIImage *selImage = [[UIImage imageNamed:imageString] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    controller.tabBarItem.selectedImage = selImage;
    
    // 修改文字的(属性)颜色
    [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor yellowColor]} forState:UIControlStateSelected];
    
    // 实例化导航控制器
    LFBNavViewController *nav = [[LFBNavViewController alloc] initWithRootViewController:controller];

    
    return nav;
    
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if(viewController == [tabBarController.viewControllers objectAtIndex:2])
    {
        LFBShopViewController *Shop = [LFBShopViewController new];
        
        // 实例化导航控制器
        LFBNavViewController *nav = [[LFBNavViewController alloc] initWithRootViewController:Shop];
        Shop.title = @"购物车";
        
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }
 
    return YES;
}


//代理
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        [self animation:index and:arry];
        
        self.indexFlag = index;
    }
}


//动画效果.
- (void)animation:(NSInteger ) index and:(NSArray *)arry{
//放大效果，并回到原位
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//速度控制函数，控制动画运行的节奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;       //执行时间
animation.repeatCount = 1;      //执行次数
animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
[[arry[index] layer] addAnimation:animation forKey:nil];
}
@end
